package selenium;

import org.testng.*;
import org.testng.annotations.*;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.List;

@Test
public class SearchFilm extends selenium.pages.TestBase {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();
  

public void testSearchFilm() throws Exception { 
	
	WebDriverWait wait = new WebDriverWait(driver, 30); //30 seconds
	
	driver.get(baseUrl + "/php4dvd/");
	
	selenium.LoginTest.testLogin();    
    
 // Search returns nothing   
    
    WebElement Search = 
    driver.findElement(By.xpath("//input[@type='text']"));
    Search.sendKeys("Avatar" + Keys.RETURN);
    
    WebElement Results = 
    driver.findElement(By.cssSelector("div.content"));	
    wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("div.content")));
    Assert.assertTrue(Results.getText().contains("No movies where found"));
    
 // Search returns results   
    
    Search.clear();
    Search.sendKeys("Man" + Keys.RETURN);    
    
    List<WebElement> Results2 = 
    wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[@id='results']/a")));    
    Assert.assertTrue(isElementPresent(By.xpath("//div[@id='results']//div[@class='title']")));  
        
   }  
	

private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
  
@AfterMethod
  public static void SiteLogout() throws Exception {
	
	selenium.LogoutTest.testLogout();
	       
  }  
  
}